package gui;

import javax.swing.*;

import calculation.MatrixLibrary;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import common.*;

public class DialogRenameFile extends JFrame {
	private MyLabel label = new MyLabel(new ImageIcon("./image/dialogRenameMatrix.png"));
	static String matrixName = "Matrix A";
	private static String oldName = null;
	static String check = "Matrix selected rename to: ";

	String matrixRename = "Rename matrix to: ";
	public static JLabel warning = new JLabel(check);

	private JLabel rename = new JLabel(oldName);
	private MyButton ok = new MyButton(new ImageIcon("./image/ok.png"));
	private MyButton cancel = new MyButton(new ImageIcon("./image/cancel.png"));
	private MyTextField text1 = new MyTextField();
	FrameMain f;

	private String newName = "Default.mtx";

	public DialogRenameFile(FrameMain main) {
		f = main;
		setSize(451, 220);
		setResizable(false);

		label.setBounds(0, 0, 451, 220);

		warning.setBounds(90, 65, 451, 25);

		rename.setBounds(90, 85, 451, 25);
		text1.setBounds(205, 85, 125, 25);
		ok.setBounds(30, 120, 154, 52);
		cancel.setBounds(255, 120, 154, 52);

		label.add(warning);
		label.add(rename);
		label.add(text1);
		label.add(ok);
		label.add(cancel);
		this.add(label);

		MouseListener listenerOk = new MouseListener() {
			public void mouseEntered(MouseEvent e) {
			}

			public void mouseClicked(MouseEvent arg0) {
				String temp;
				if ((temp = text1.getText()) != null) {
					newName = temp;
				}
				f.getFileHandler().renameFile(oldName, newName);

				f.getInputHandler().loadMatricesFromLibraryDirectory();
				String[] nameList = (String[]) MatrixLibrary.getNameList().toArray(new String[0]);
				f.getPanelDisplayMatrixList().updateMyLayout(nameList);
				f.getPanelCalculationBar().setMatrixNameList(nameList);

				f.getDialogRenameFile().setVisible(false);

			}

			public void mouseExited(MouseEvent arg0) {
			}

			public void mousePressed(MouseEvent arg0) {
			}

			public void mouseReleased(MouseEvent arg0) {
			}
		};
		ok.addMouseListener(listenerOk);
		MouseListener listenerCancel = new MouseListener() {
			public void mouseEntered(MouseEvent e) {
			}

			public void mouseClicked(MouseEvent arg0) {
				f.getDialogRenameFile().setVisible(false);
			}

			public void mouseExited(MouseEvent arg0) {
			}

			public void mousePressed(MouseEvent arg0) {
			}

			public void mouseReleased(MouseEvent arg0) {
			}
		};
		cancel.addMouseListener(listenerCancel);
	}

	public static void setTarget(String name) {
		oldName = name;
	}
}
